import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

class MyThreadPoolExecutor{
    private List<Thread> threadList=new ArrayList<>();

    //阻塞队列
    private BlockingQueue<Runnable> queue=new ArrayBlockingQueue<>(10);

    public MyThreadPoolExecutor(int n){
        for(int i=0;i<n;i++){
            Thread t=new Thread(()-> {
                while (true) {
                    try {
                        //take操作也带有阻塞
                        Runnable runnable = queue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            t.start();
            threadList.add(t);
        }
    }

    public void submit(Runnable runnable) throws InterruptedException {
        //put操作带有阻塞功能
        queue.put(runnable);
    }
}
public class ThreadDemo6 {
    public static void main(String[] args) throws InterruptedException {
       MyThreadPoolExecutor executor=new MyThreadPoolExecutor(4);
       for(int i=0;i<1000;i++){
           int n=i;
           executor.submit(new Runnable() {
               @Override
               public void run() {
                   System.out.println("执行任务："+n+",当前线程："+
                           Thread.currentThread().getName());
               }
           });
       }
    }
}
